﻿package com.mttang.utils{

	import flash.display.Sprite;
	import flash.events.*;
	import flash.display.DisplayObject;
	import flash.geom.Rectangle;

	public class ScrollPane extends Sprite {
		private var displayList:DisplayObject;
		private var movePoint:Rectangle;
		private var scale:Number;

		public function ScrollPane() {
		}
		public function show(_display:DisplayObject):void {
			if (displayList!=null) {
				list.removeChild(displayList);
			}
			displayList=_display;
			list.addChild(displayList);
			list.mask=scroll_mask;
			list.x=0;
			list.y=0;
			if (displayList.height>scroll_mask.height) {
				InitShow();
			} else {
				scroll_mc.visible=false;
			}
		}
		public function setSizeWH(w:Number, h:Number):void {
			scroll_mask.width=w;
			scroll_mask.height=h;
			/*if (displayList!=null) {
				if (displayList.width>w) {
					scroll_mask.width=w;
				}
				if (displayList.height>h) {
					scroll_mask.height=h;
				}
			}*/
		}
		private function InitShow():void {
			scroll_mc.visible=true;
			scroll_mask.y=scroll_mc.y=scroll_mc.scroll_line.y=list.y;
			scroll_mc.scroll_line.height=scroll_mask.height;
			scale=(list.height-scroll_mask.height)/list.height;
			scroll_mc.scroll_bar.height=scroll_mc.scroll_line.height*(1-scale);
			scroll_mc.scroll_bar.y=scroll_mc.scroll_line.y;
			scroll_mc.x = scroll_mask.width + 2;
			//scroll_up.x = scroll_mc.x;
			//scroll_up.y = scroll_mc.y - 6;
			//scroll_down.x = scroll_mc.x;
			//scroll_down.y = scroll_mc.height + 2;
			
			

			movePoint=new Rectangle(scroll_mc.scroll_line.x,scroll_mc.scroll_line.y,0,scroll_mc.scroll_line.height-scroll_mc.scroll_bar.height);
			scroll_mc.scroll_bar.addEventListener(MouseEvent.MOUSE_DOWN, onBarBtnonHandler);
			scroll_mc.scroll_bar.useHandCursor=false;

		}

		//------------------------------------------------------------------------------ScrollBarMove---------------------------------------------------
		private function onBarBtnonHandler(e:MouseEvent):void {
			e.currentTarget.startDrag(false, movePoint);
			e.currentTarget.stage.addEventListener(MouseEvent.MOUSE_UP, stopMoveHandler);
			e.currentTarget.stage.addEventListener(Event.ENTER_FRAME, onBar_BtnEnterFrame);

		}

		private function stopMoveHandler(e:MouseEvent):void {
			scroll_mc.scroll_bar.stopDrag();
			e.currentTarget.removeEventListener(MouseEvent.MOUSE_UP, stopMoveHandler);
			e.currentTarget.stage.removeEventListener(Event.ENTER_FRAME, onBar_BtnEnterFrame);

		}

		private function onBar_BtnEnterFrame(e:Event):void {
			onBarDragMovie();
		}

		private function onBarDragMovie():void {
			var H:Number=list.height-scroll_mask.height;
			var h:Number=scroll_mc.scroll_bar.y-scroll_mc.scroll_line.y;
			var h1:Number=scroll_mc.scroll_line.height-scroll_mc.scroll_bar.height;
			list.y=- H*h/h1;
		}
		//------------------------------------------------------------------------------------------------------------------------------------------------------- 
	}
}